package com.taru.order.controller;


import com.taru.order.common.Data;
import com.taru.order.common.JsonResult;
import com.taru.order.entity.Note;
import com.taru.order.entity.NoteLikes;
import com.taru.order.service.NoteServiceImpl;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
@Controller
public class NoteController {

    NoteServiceImpl noteService=new NoteServiceImpl();

    /**
     * 添加小记
     */
    @RequestMapping(value = "/notes",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addNote(@RequestBody Note note){
        JsonResult result=null;
        try{
            int code=noteService.addNote(note);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改小记
     */
    @RequestMapping(value = "/notes",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modNote(@RequestBody Note note){
        JsonResult result=null;
        try{
            int code=noteService.modNote(note);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 批量删除小记
     */
    @RequestMapping(value = "/notes",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeNotes( @RequestBody  List<Integer> list){
        JsonResult result=null;
        try{
            int code=noteService.removeNotes(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 增加小记的点赞量
     */
    @RequestMapping(value = "/notes/inLikes",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult inNoteLikes( @RequestBody NoteLikes likes){
        JsonResult result=null;
        try{
            int code=noteService.inNoteLikes(likes);
            if(code>0){
                result =new JsonResult(200,"增加成功");
            }else {
                result=new JsonResult(404,"增加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 减少小记的点赞量
     */
    @RequestMapping(value = "/notes/outLikes",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult outNoteLikes(@RequestBody  NoteLikes likes){
        JsonResult result=null;
        try{
            int code=noteService.outNoteLikes(likes);
            if(code>0){
                result =new JsonResult(200,"减少成功");
            }else {
                result=new JsonResult(404,"点赞量已为0");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 判断小记有没有被点赞过
     */
    @RequestMapping(value = "/notes/likes/judge",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult queryNoteLikes( @RequestBody NoteLikes likes){
        JsonResult result=null;
        try{
            NoteLikes likes1=noteService.queryNoteLikes(likes);
            if(likes1!=null){
                result =new JsonResult(200,"获取成功",true);
            }else{
                result =new JsonResult(200,"获取成功",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 获取小记详情
     */
    @RequestMapping(value = "/notes/publish/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listPublishNotes(@PathVariable("id") String id){
        JsonResult result=null;
        try{
            Note note=noteService.getNoteById(id);
            if(note!=null){
                result =new JsonResult(200,"获取成功",note);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 分页获取小记列表
     */
    @RequestMapping(value = "/notes",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listNotes(Integer pageNum, Integer pageSize){
        JsonResult result=null;
        try{
            Data data=noteService.listNotes(pageNum,pageSize);
            if(data !=null){
                result =new JsonResult(200,"获取成功",data);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 根据用户id查询小记列表
     */
    @RequestMapping(value = "/notes/{userId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listNotes(@PathVariable("userId") String userId){
        JsonResult result=null;
        try{
            List list=noteService.getNoteByUserId(userId);
            if(list !=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按照时间查询小记列表
     */
    @RequestMapping(value = "/notes/time",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listNotes(String year,String month){
        JsonResult result=null;
        try{
            List list=noteService.getNoteByYearMonth(year,month);
            if(list !=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
